package com.lanou3g.ptp.achievement.service.impl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.achievement.mapper.SummaryMapper;
import com.lanou3g.ptp.achievement.service.SummaryService;
import com.lanou3g.ptp.achievement.word.ExportWord;
import com.lanou3g.ptp.entity.Message;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Summary;
import com.lanou3g.ptp.message.service.MessageService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.utils.DateUtils;
import com.lanou3g.ptp.vo.AlertBody;
import com.lanou3g.ptp.vo.TemplateData;
import com.lanou3g.ptp.vo.WxTmplMsg;
import com.lanou3g.ptp.wx.service.WxService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

//import com.lanou3g.utils.Word;

@com.alibaba.dubbo.config.annotation.Service
public class SummaryServiceImpl implements SummaryService {

    @Autowired
    SummaryMapper summaryMapper;
//    @Reference
//    private TaskService taskService;
//    @Reference
//    private SchoolService schoolService;
//    @Reference
//    private StudentService studentService;

    @Reference(lazy = true)
    private StudentService studentService;
    @Reference(lazy = true)
    private WxService wxService;
    @Reference(lazy = true)
    private MessageService messageService;

    @Override
    public PageInfo<Summary> findAllSummary(Integer page, Integer size) {

        PageHelper.startPage(page, size);
        List<Summary> all = summaryMapper.findAll();
        return new PageInfo<>(all);
    }

    @Override
    public List<Summary> findAll1Summary() {
        return summaryMapper.findAll1();
    }

    @Override
    public Summary findSummaryById(Long id) {

        return summaryMapper.findSummaryById(id);
    }

    @Override
    public PageInfo<Summary> findByKeyword(Integer page, Integer size, String keyword) {

        PageHelper.startPage(page, size);
        List<Summary> byKeyword = summaryMapper.findByKeyword("%" + keyword + "%");
        return new PageInfo<>(byKeyword);
    }

    @Override
    public boolean insert(Summary summary) {
        int count = 0;
        Date date = new Date();
        summary.setCreateTime(date);
        summary.setUpdateTime(date);
        summary.setStatus(0);

        count = summaryMapper.findCount(summary.getStudentId(), summary.getTaskId());

        if (count == 0) {
            int i = summaryMapper.insert(summary);
            return i > 0;
        }
        return false;


    }

    @Override
    public void updateSummary(Long id, Summary summary) {
        summary.setUpdateTime(new Date());
        summaryMapper.updateSummary(id, summary);
    }

    @Override
    public void deleteSummaryId(Long id) {
        summaryMapper.deleteSummaryId(id);
    }

    @Override
    public void deleteSummaryIds(Long[] ids) {
        summaryMapper.deleteSummaryIds(ids);
    }

    @Override
    public List<Summary> MyFindAll(Long id) {

        return summaryMapper.myFindAll(id);
    }

    @Override
    public Summary findByTime(Long taskId, Long studentId) {

//        return summaryMapper.findByTime(taskId, studentId);

        Summary summary = summaryMapper.findByTime(taskId, studentId);

        return summary;
    }

    //导出word
    @Override
    public void newWord(Map<String, Object> result) throws IOException {

//        HashMap<String, Object> result = new HashMap<>();
//
//        Summary summary = summaryMapper.findSummaryById(id);
//        result.put("summary",summary);
//
//        Task task = taskService.findById(summary.getTaskId());
//        result.put("taskName",task.getName());
//
//        Student student = studentService.studentById(summary.getStudentId());
//        result.put();

//
//        School school = schoolService.findById();
//        result.put("schoolName",school.getName());


        ExportWord ew = new ExportWord();
        XWPFDocument document = ew.createXWPFDocument(result);
        List<List<Object>> list = new ArrayList<List<Object>>();
        Map<String, Object> dataList = new HashMap<String, Object>();
        dataList.put("TITLE", "");
        dataList.put("TABLEDATA", list);
        ew.exportCheckWord(dataList, document, "D:\\word\\summary000000.docx");
    }

    @Override
    public boolean alertSummary(AlertBody alert) {
        Student student = this.studentService.findById(alert.getStudentId());
        Map<String, TemplateData> data = new HashMap<>();
        String content = "请提交总结报告！（已提交报告请忽略）";
        Date date = DateUtils.endTime(new Date());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String s = df.format(date);
        data.put("keyword1", new TemplateData("总结提交"));
        data.put("keyword2", new TemplateData(content));
        data.put("keyword3", new TemplateData(s));
        data.put("keyword4", new TemplateData("请尽快提交总结提交!"));
        boolean result = wxService.sendMessage(student.getOpenId(), WxTmplMsg.TYPE_SUMMARY, data);
        if (result) {
            Message message = new Message();
            message.setStudentId(student.getId());
            message.setTaskId(alert.getTaskId());
            message.setContent(content);
            return messageService.saveSummaryMessage(message);
        }
        return false;
    }

    @Override
    public Summary findMySummaryByTaskId(Long studentId, Long taskId) {
        return summaryMapper.findMySummaryByTaskId(studentId, taskId);
    }

    @Override
    public void excellentReport(Long id) {
        this.summaryMapper.excellentSummary(id);
    }
}
